// 日期类型转换
export const datetimeToStr = (dateObj, noTime, sep) => {
  if (!dateObj) {
    return null
  }
  if (!sep && sep !== '') {
    sep = '/'
  }
  const dateItem = new Date(dateObj)
  const y = dateItem.getFullYear()
  const m = (dateItem.getMonth() + 1 + '').padStart(2, '0')
  const d = (dateItem.getDate() + '').padStart(2, '0')
  const hh = (dateItem.getHours() + '').padStart(2, '0')
  const mm = (dateItem.getMinutes() + '').padStart(2, '0')
  const ss = (dateItem.getSeconds() + '').padStart(2, '0')
  if (noTime) {
    return `${y}${sep}${m}${sep}${d}`
  } else {
    return `${y}${sep}${m}${sep}${d} ${hh}:${mm}:${ss}`
  }
}

// -年-月-日-星期几
export const getDateWeek = (dateObj) => {
  const date = new Date(dateObj)
  let week
  switch (date.getDay()) {
    case 1:
      week = '星期一'
      break
    case 2:
      week = '星期二'
      break
    case 3:
      week = '星期三'
      break
    case 4:
      week = '星期四'
      break
    case 5:
      week = '星期五'
      break
    case 6:
      week = '星期六'
      break
    default:
      week = '星期日'
  }
  const y = date.getFullYear()
  const m = (date.getMonth() + 1 + '').padStart(2, '0')
  const d = (date.getDate() + '').padStart(2, '0')
  return `${y}年${m}月${d}日 ${week}`
}

// 获取一年的日期
export const getDateOfYear = () => {
  let dateList = []
  let dateCount
  for (let m = 1;m<=12;m++){
    if ([1,3,5,7,8,10,12].indexOf(m)>-1){
      dateCount = 31
    }else if(m === 2){
      dateCount = 28
    }else{
      dateCount = 30
    }
    for (let d=1;d<=dateCount;d++){
      dateList.push((m+'').padStart(2, '0') + '-' + (d+'').padStart(2, '0'))
    }
  }
  return dateList
}

// 解析int类型的时间
export const parseIntegerTime = (time) => {
  let timeStr = time + ''
  const year = parseInt(timeStr.slice(0,4))
  const month = parseInt(timeStr.slice(4,6)) - 1
  const day = parseInt(timeStr.slice(6,8))
  return new Date(year, month, day)
}

// 获取月份日期的起始
export const getMonthDatePoint = (date, mode, stringfy) => {
  if (!mode){
    mode = 'current'
  }
  let monthStart;
  let monthEnd;
  let paramDate = new Date(date)
  if (mode === 'previous'){ // 上个月
    monthStart = new Date(paramDate.getFullYear(), paramDate.getMonth() - 1, 1)
    monthEnd = new Date(new Date(paramDate.getFullYear(), paramDate.getMonth(), 1).getTime() - 1)
  }else if (mode === 'next'){
    monthStart = new Date(paramDate.getFullYear(), paramDate.getMonth() + 1, 1)
    monthEnd = new Date(new Date(paramDate.getFullYear(), paramDate.getMonth() + 2, 1).getTime() - 1)
  }else{
    // 默认当前
    monthStart = new Date(paramDate.getFullYear(), paramDate.getMonth(), 1)
    monthEnd = new Date(new Date(paramDate.getFullYear(), paramDate.getMonth()+1, 1).getTime() - 1)
  }
  if (stringfy || stringfy === ''){
    monthStart = datetimeToStr(monthStart, true, stringfy)
    monthEnd = datetimeToStr(monthEnd, true, stringfy)
  }
  return [monthStart, monthEnd]
}

// 获取一年的起始
export const getYearDatePoint = (year, mode, stringfy) => {
  if (!mode) {
    mode = 'current'
  }
  let yearStart
  let yearEnd
  if (mode === 'previous') {
    // 上一年
    yearStart = new Date(year - 1, 0, 1)
    yearEnd = new Date(year - 1, 11, 31)
  } else if (mode === 'next') {
    yearStart = new Date(year + 1, 0, 1)
    yearEnd = new Date(year + 1, 11, 31)
  } else {
    yearStart = new Date(year, 0, 1)
    yearEnd = new Date(year, 11, 31)
  }
  if (stringfy || stringfy === '') {
    yearStart = datetimeToStr(yearStart, true, stringfy)
    yearEnd = datetimeToStr(yearEnd, true, stringfy)
  }
  return [yearStart, yearEnd]
}

// 获取昨日
export const getYesterday = (formater, noTime, sep) => {
  const yesterday = new Date((new Date().getTime() - 86400 * 1000))
  return formater?datetimeToStr(yesterday, noTime, sep):yesterday
}

// 获取年份数据
export const getYearList = (start, reverse) => {
  const nowYear = new Date().getFullYear()
  if (!start){
    start = new Date().getFullYear()
  }
  let yearList = []
  for (let i=start;i<=nowYear;i++){
    yearList.push(i)
  }
  if (new Date().getMonth() >= 3){
    yearList.push(nowYear + 1)
  }
  
  return reverse?yearList.reverse():yearList
}
